package jmind.core.ibatis;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import jmind.core.lang.IProperties;
import jmind.core.util.DataUtil;

import org.apache.commons.dbcp.BasicDataSource;

public abstract class AbstractDataSource {
	
	private final Map<String, BasicDataSource> dbs = new ConcurrentHashMap<String, BasicDataSource>();

    public BasicDataSource getResource(final String name) {
    	BasicDataSource db = dbs.get(name);
        if (db == null) {
            synchronized (dbs) {
                if (dbs.containsKey(name)) {
                    return dbs.get(name);
                } else {
                    db = createSource(name);
                    dbs.put(name, db);
                }
            }

        }
        return db;
    }

	public abstract IProperties getProperties();
    private BasicDataSource createSource(String name) {
        /**
         * <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.user}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxIdle" value="10" />
        <!-- 300秒回收不用的连接 -->
        <property name="removeAbandoned" value="true" />
        <property name="removeAbandonedTimeout" value="300" />
        <!-- MySQL8小时问题 -->
        <property name="validationQuery" value="SELECT 1" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <property name="testWhileIdle" value="true" />
        <property name="timeBetweenEvictionRunsMillis" value="300000" />
        <property name="numTestsPerEvictionRun" value="3" />
        <property name="minEvictableIdleTimeMillis" value="1800000" />
        
         */
    	IProperties p = getProperties();
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        String url = p.getProperty("jdbc." + name + ".url");
        System.out.println("jdbc." + name + ".url=" + url);
        ds.setUrl(url);
        ds.setUsername(p.getProperty("jdbc." + name + ".username"));
        ds.setPassword(p.getProperty("jdbc." + name + ".password"));
        ds.setMaxIdle(DataUtil.toInt(p.getProperty("jdbc." + name + ".maxIde", "50")));
        ds.setRemoveAbandoned(true); //是否自动回收超时连接  
        ds.setRemoveAbandonedTimeout(300); //单位秒
        ds.setLogAbandoned(true); //自动中断写日志
        ds.setValidationQuery("SELECT 1");
        ds.setTestOnBorrow(false);
        ds.setTestOnReturn(false);

        ds.setMaxActive(300);

        ds.setTestWhileIdle(true); //指明连接是否被空闲连接回收
        ds.setTimeBetweenEvictionRunsMillis(300000); //在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位.
        ds.setNumTestsPerEvictionRun(3); //在每次空闲连接回收器线程(如果有)运行时检查的连接数量
        ds.setMinEvictableIdleTimeMillis(1800000); //连接池中连接可空闲的时间,毫秒

        if (name.contains("slave"))
            ds.setDefaultReadOnly(true);
        return ds;
    }
}
